<html>
<head><meta charset="utf-8"><title>Rust-lld generates wrong thunk code · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html">Rust-lld generates wrong thunk code</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="226347211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226347211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226347211">(Feb 15 2021 at 05:40)</a>:</h4>
<p>I've been trying to get use multicore on an embedded device (LPC55S69). I followed jorge's <a href="https://github.com/japaric/lpcxpresso55S69">https://github.com/japaric/lpcxpresso55S69</a> repo and with some minor modifications got the examples working. With some google-fu, I got gdb debugging working on both cores as well.</p>
<p>But after that, I tried to call the same function from both the cores.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![no_main]</span><span class="w"></span>
<span class="cp">#![no_std]</span><span class="w"></span>

<span class="k">use</span><span class="w"> </span><span class="n">lpc55s6x</span>::<span class="p">{</span><span class="n">BLUE</span><span class="p">,</span><span class="w"> </span><span class="n">GREEN</span><span class="p">};</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span><span class="n">panic_halt</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">_</span><span class="p">;</span><span class="w"></span>

<span class="cp">#[inline(never)]</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">toggle</span><span class="p">(</span><span class="n">col</span>: <span class="kt">u8</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">col</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">GREEN</span><span class="p">.</span><span class="n">toggle</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">BLUE</span><span class="p">.</span><span class="n">toggle</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[no_mangle]#[link_section = </span><span class="s">".text_0.1"</span><span class="cp">]</span><span class="w"></span>
<span class="k">unsafe</span><span class="w"> </span><span class="k">extern</span><span class="w"> </span><span class="s">"C"</span><span class="w"> </span><span class="k">fn</span> <span class="nf">main_0</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="o">!</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">GREEN</span><span class="p">.</span><span class="n">on</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="k">loop</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">for</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">1</span><span class="o">..</span><span class="mi">10000</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">        </span><span class="c1">// GREEN.toggle();</span>
<span class="w">        </span><span class="n">toggle</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[no_mangle]</span><span class="w"></span>
<span class="cp">#[link_section = </span><span class="s">".text_1.1"</span><span class="cp">]</span><span class="w"></span>
<span class="k">unsafe</span><span class="w"> </span><span class="k">extern</span><span class="w"> </span><span class="s">"C"</span><span class="w"> </span><span class="k">fn</span> <span class="nf">main_1</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="o">!</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">BLUE</span><span class="p">.</span><span class="n">on</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="k">loop</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">for</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">1</span><span class="o">..</span><span class="mi">10000</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">        </span><span class="c1">// BLUE.toggle();</span>
<span class="w">        </span><span class="n">toggle</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>This code panics on 2nd core but 1st core runs fine.</p>
<p>The cause of panic is also confusing but I think y'all may be able to shed some light.</p>
<p>The backtrace is</p>
<div class="codehilite"><pre><span></span><code>(gdb) bt
#0  lpc55s6x::DefaultHandler () at /home/vn-ki/git/harsark-lpc55S69-multicore-examples/src/lib.rs:330
#1  &lt;signal handler called&gt;
#2  0x2001013c in led::main_1 () at /home/vn-ki/git/harsark-lpc55S69-multicore-examples/examples/led.rs:37
#3  0x2001016c in lpc55s6x::_start_1 () at /home/vn-ki/git/harsark-lpc55S69-multicore-examples/src/lib.rs:263
#4  0xfffffffe in ?? ()
</code></pre></div>
<p>main_1:</p>
<div class="codehilite"><pre><span></span><code>(gdb) disass led::main_1
Dump of assembler code for function led::main_1:
   0x20010130 &lt;+0&gt;:     push    {r7, lr}
   0x20010132 &lt;+2&gt;:     mov     r7, sp
   0x20010134 &lt;+4&gt;:     ldr     r0, [pc, #12]   ; (0x20010144 &lt;led::main_1+20&gt;)
   0x20010136 &lt;+6&gt;:     movs    r1, #16
   0x20010138 &lt;+8&gt;:     str     r1, [r0, #0]
   0x2001013a &lt;+10&gt;:    movs    r0, #1
   0x2001013c &lt;+12&gt;:    blx     0x20010174 &lt;__ARMv5ABSLongThunk__ZN3led6toggle17h8b94eb1569c0cb62E&gt;
   0x20010140 &lt;+16&gt;:    b.n     0x2001013a &lt;led::main_1+10&gt;
   0x20010142 &lt;+18&gt;:    nop
   0x20010144 &lt;+20&gt;:    b.n     0x20010650
   0x20010146 &lt;+22&gt;:    ands    r0, r1
</code></pre></div>
<div class="codehilite"><pre><span></span><code>(gdb) disass 0x20010174
Dump of assembler code for function __ARMv5ABSLongThunk__ZN3led6toggle17h8b94eb1569c0cb62E:
   0x20010174 &lt;+0&gt;:     bfcsel  0, 0x20010bb4, 2, ne
   0x20010178 &lt;+4&gt;:     lsls    r1, r1, #5
   0x2001017a &lt;+6&gt;:     movs    r0, r0
End of assembler dump.
(gdb) x/16bx 0x20010174
0x20010174 &lt;__ARMv5ABSLongThunk__ZN3led6toggle17h8b94eb1569c0cb62E&gt;:    0x04    0xf0    0x1f    0xe5    0x49   0x01     0x00    0x00
</code></pre></div>
<p>Those instructions did not make any sense, so I opened the elf in ghidra and</p>
<div class="codehilite"><pre><span></span><code>                                 *************************************************************************
                                 *                            THUNK FUNCTION                             *
                                 *************************************************************************
                                 thunk undefined __ARMv5ABSLongThunk__ZN3led6toggle17h8b94eb1569c0c
                                     Thunked-Function: led::toggle::h8b94eb1569c0cb62
               undefined            r0:1             &lt;RETURN&gt;
                                 __ARMv5ABSLongThunk__ZN3led6toggle17h8b94eb1569c0cb62E  XREF[1]:       main_1:2001013c(c)
           20010174 04 f0 1f e5       ldr          pc=&gt;led::toggle::h8b94eb1569c0cb62,[DAT_20010178]        = 00000149h
                                 DAT_20010178                                            XREF[1]:       __ARMv5ABSLongThunk__ZN3led6toggle17h8
           20010178 49 01 00 00       undefined4   00000149h
</code></pre></div>
<p>I don't really understand why this is happening (?) Is this compiler confused and generating incompatible code?</p>
<p>Upon further investigation this <code>__ARMv5</code> problem affects a lot more things. For example, I can't print anything from core 1 because the hprintln compiles down to</p>
<div class="codehilite"><pre><span></span><code> 0x20010162  led::main_1+50 movs    r0, #1
 0x20010164  led::main_1+52 blx    0x200101e4 &lt;__ARMv5ABSLongThunk___c_m_sh_syscall&gt;
....
&gt;&gt;&gt; disass 0x200101e4
Dump of assembler code for function __ARMv5ABSLongThunk___c_m_sh_syscall:
   0x200101e4 &lt;+0&gt;:     bfcsel  0, 0x20010c24, 2, ne
   0x200101e8 &lt;+4&gt;:     lsls    r7, r3, #9
   0x200101ea &lt;+6&gt;:     movs    r0, r0
</code></pre></div>
<p>which panics</p>



<a name="226347228"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226347228" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226347228">(Feb 15 2021 at 05:41)</a>:</h4>
<p>.cargo/config</p>
<div class="codehilite"><pre><span></span><code>[target.&#39;cfg(all(target_arch = &quot;arm&quot;, target_os = &quot;none&quot;))&#39;]
runner = &quot;arm-none-eabi-gdb -q -x jlink.gdb&quot;

rustflags = [
  &quot;-C&quot;, &quot;link-arg=-Tlink.x&quot;,
]

[build]
target = &quot;thumbv8m.main-none-eabi&quot;
</code></pre></div>



<a name="226489326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226489326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226489326">(Feb 16 2021 at 09:21)</a>:</h4>
<p>I tried switching from rust-lld to gnu ld and it seems to generate the correct code but there are (unknown issue) which is causing the core to not boot. When I manually boot the core, the thunk (/veneer) works correctly</p>
<p>What gnu ld generates:</p>
<div class="codehilite"><pre><span></span><code>20010178 &lt;___ZN3led6toggle17h27d172181cf2f671E_veneer&gt;:
20010178:       b401            push    {r0}
2001017a:       4802            ldr     r0, [pc, #8]    ; (20010184 &lt;___ZN3led6toggle17h27d172181cf2f671E_veneer+0xc&gt;)
2001017c:       4684            mov     ip, r0
2001017e:       bc01            pop     {r0}
20010180:       4760            bx      ip
20010182:       bf00            nop
20010184:       00000149        .word   0x00000149
</code></pre></div>



<a name="226489503"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226489503" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226489503">(Feb 16 2021 at 09:22)</a>:</h4>
<p>So this confirms that it has to do with rust-lld</p>
<p>I looked at some cli options of lld to see if there is some flag that could force it to output the correct stub code but couldnt find any.</p>



<a name="226489536"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226489536" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226489536">(Feb 16 2021 at 09:23)</a>:</h4>
<p>Just a guess, but is it possible that LLD create a non-thumb thunk?</p>



<a name="226490236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226490236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226490236">(Feb 16 2021 at 09:29)</a>:</h4>
<p>Hmm, that's a possibility. Wonder how lld chooses to generate which version.</p>



<a name="226493371"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226493371" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226493371">(Feb 16 2021 at 10:00)</a>:</h4>
<p>Another observation (that may or may not be helpful) is that the name of the thunk generated by LLD says ARMv5. The board I'm using have Cortex M-33 cores which is ARMv8 AFAIK</p>



<a name="226494301"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226494301" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226494301">(Feb 16 2021 at 10:08)</a>:</h4>
<p>32-bit arm architecture is backwards compatible, core that's capable executing v8 will execute v5 just as fine (provided its in the ARM mode)</p>



<a name="226494489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226494489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226494489">(Feb 16 2021 at 10:09)</a>:</h4>
<p>but presence of v5 would suggest that your veneer is indeed ARM, not thumb.</p>



<a name="226494616"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226494616" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226494616">(Feb 16 2021 at 10:10)</a>:</h4>
<p>Does this mean that my core 0 code is thumb and core 1 code is ARM?</p>



<a name="226494770"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226494770" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226494770">(Feb 16 2021 at 10:12)</a>:</h4>
<p>ARM cores switch modes almost transparently (based on whether the last bit is set when the function is called). The only case I can think where this could cause issues is maybe some MSR flags that control whether thumb is enabled or not.</p>



<a name="226494933"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226494933" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226494933">(Feb 16 2021 at 10:14)</a>:</h4>
<p>You should probably set up exception handlers for at least some common hardware interrupts cores might execute when encountering invalid code: usage fault, bus fault, etc.</p>



<a name="226495015"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226495015" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226495015">(Feb 16 2021 at 10:15)</a>:</h4>
<p>(you can still figure out what exception you're executing without doing so, but its more difficult than just running <code>bt</code> and involves looking at certain registers)</p>



<a name="226495576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226495576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226495576">(Feb 16 2021 at 10:21)</a>:</h4>
<p>I don't understand why objdump shows</p>
<div class="codehilite"><pre><span></span><code>20010178 &lt;__ARMv5ABSLongThunk__ZN3led6toggle17h27d172181cf2f671E&gt;:
20010178:       e51ff004        ldr     pc, [pc, #-4]   ; 2001017c &lt;__ARMv5ABSLongThunk__ZN3led6toggle17h27d172181cf2f671E+0x4&gt;
2001017c:       00000149        .word   0x00000149
</code></pre></div>
<p>while gdb (connected to the board)) shows</p>
<div class="codehilite"><pre><span></span><code>(gdb) disass 0x20010178
Dump of assembler code for function __ARMv5ABSLongThunk__ZN3led6toggle17h27d172181cf2f671E:
   0x20010178 &lt;+0&gt;:     bfcsel  0, 0x20010bb8, 2, ne
   0x2001017c &lt;+4&gt;:     lsls    r1, r1, #5
   0x2001017e &lt;+6&gt;:     movs    r0, r0
End of assembler dump.
</code></pre></div>



<a name="226495685"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226495685" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226495685">(Feb 16 2021 at 10:22)</a>:</h4>
<p>What does <code>disass/r 0x20010178</code> show?</p>



<a name="226495738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226495738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226495738">(Feb 16 2021 at 10:23)</a>:</h4>
<div class="codehilite"><pre><span></span><code>&gt;&gt;&gt; disass/r 0x20010178
Dump of assembler code for function __ARMv5ABSLongThunk__ZN3led6toggle17h27d172181cf2f671E:
   0x20010178 &lt;+0&gt;:     04 f0 1f e5     bfcsel  0, 0x20010bb8, 2, ne
   0x2001017c &lt;+4&gt;:     49 01   lsls    r1, r1, #5
   0x2001017e &lt;+6&gt;:     00 00   movs    r0, r0
End of assembler dump.
</code></pre></div>



<a name="226495815"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226495815" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226495815">(Feb 16 2021 at 10:24)</a>:</h4>
<p>gdb may be disassembling in thumb mode. Oftentimes what mode a tool will disassemble with is based on heuristics.</p>



<a name="226495900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226495900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226495900">(Feb 16 2021 at 10:25)</a>:</h4>
<p>try</p>
<div class="codehilite"><pre><span></span><code>show disassembly-flavor
</code></pre></div>



<a name="226495941"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226495941" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226495941">(Feb 16 2021 at 10:25)</a>:</h4>
<p>ah, no that's not the right thing</p>



<a name="226496122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226496122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226496122">(Feb 16 2021 at 10:27)</a>:</h4>
<p><a href="https://www-zeuthen.desy.de/unix/unixguide/infohtml/gdb/ARM.html">https://www-zeuthen.desy.de/unix/unixguide/infohtml/gdb/ARM.html</a> <code>arm fallback-mode</code> and <code>arm force-mode</code> are relevant.</p>



<a name="226496201"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226496201" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226496201">(Feb 16 2021 at 10:28)</a>:</h4>
<p>From this:</p>
<blockquote>
<p>The default is `auto', which causes gdb to use the current execution mode (from the T bit in the CPSR register).</p>
</blockquote>



<a name="226496452"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226496452" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226496452">(Feb 16 2021 at 10:30)</a>:</h4>
<p>So that points out a couple of interesting things: first, your core _may_ be executing code in wrong mode (to make sure you would need to break on <code>__ARMv5ABSLongThunk__ZN3led6toggle17h27d172181cf2f671E</code> and disassemble before its first instruction is executed). But also, do can load your originating ELF file to let gdb know the debuginfo and symbol table of your code if you aren't already.</p>



<a name="226496481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226496481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226496481">(Feb 16 2021 at 10:30)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123586">nagisa</span> <a href="#narrow/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code/near/226494933">said</a>:</p>
<blockquote>
<p>You should probably set up exception handlers for at least some common hardware interrupts cores might execute when encountering invalid code: usage fault, bus fault, etc.</p>
</blockquote>
<p>thanks for this advice.</p>
<div class="codehilite"><pre><span></span><code>Program received signal SIGTRAP, Trace/breakpoint trap.
lpc55s6x::BusFaultHandler () at /home/vn-ki/git/harsark-lpc55S69-multicore-examples/src/lib.rs:336
336     unsafe extern &quot;C&quot; fn BusFaultHandler() -&gt; ! {
(gdb) bt
#0  lpc55s6x::BusFaultHandler () at /home/vn-ki/git/harsark-lpc55S69-multicore-examples/src/lib.rs:336
#1  &lt;signal handler called&gt;
#2  0x20010142 in led::main_1 () at /home/vn-ki/git/harsark-lpc55S69-multicore-examples/examples/led.rs:39
#3  0x20010176 in lpc55s6x::_start_1 () at /home/vn-ki/git/harsark-lpc55S69-multicore-examples/src/lib.rs:263
#4  0xfffffffe in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
</code></pre></div>



<a name="226496703"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226496703" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226496703">(Feb 16 2021 at 10:32)</a>:</h4>
<p>Do you execute anything of the <code>file target/release/firmware</code> sort when you connect to/flash your probe/device?</p>



<a name="226496822"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226496822" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226496822">(Feb 16 2021 at 10:34)</a>:</h4>
<p>(ah, you probably do, seeing as you have line numbers available to you)</p>



<a name="226497216"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226497216" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226497216">(Feb 16 2021 at 10:37)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123586">nagisa</span> <a href="#narrow/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code/near/226496703">said</a>:</p>
<blockquote>
<p>Do you execute anything of the <code>file target/release/firmware</code> sort when you connect to/flash your probe/device?</p>
</blockquote>
<p>yeah, so the debugging in this board is a bit weird (idk if this is the norm but for me it feels weird). I have to run seperate JLinkGDBServer for the 2 cores and run seperate GDB instances to debug each of the cores.</p>
<p>To answer, your question, yes</p>
<div class="codehilite"><pre><span></span><code>arm-none-eabi-gdb target/thumbv8m.main-none-eabi/release/examples/led -x jlink-core1.gdb
</code></pre></div>
<div class="codehilite"><pre><span></span><code>set print asm-demangle on

break DefaultHandler
break HardFault
break rust_begin_unwind

set history save on
set confirm off

# find commit-hash using `rustc -Vv`
set substitute-path /rustc/f74583445702e2e27ec4415376f2c540a83d7ded /home/vn-ki/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust

target extended-remote :2334
load
monitor reset
monitor semihosting enable
# monitor semihosting breakOnError &lt;digit&gt;
# by default (1) output goes to Telnet client, 2 sends to GDB client, 3 would send to both
monitor semihosting IOClient 3

#monitor SWO enabletarget 150000000 0 1 0
# mon SWO EnableTarget 0 48000000 1875000 0
# continue
# stepi

#source ~/.gdbinit-gdb-dashboard
</code></pre></div>



<a name="226497369"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226497369" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226497369">(Feb 16 2021 at 10:39)</a>:</h4>
<p>yeah I never dealt with multicore embedded devices before (if you ignore multiprocessor ones!), so can't help with jlink's behaviour in that situation <span aria-label="frown" class="emoji emoji-1f641" role="img" title="frown">:frown:</span></p>



<a name="226497538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226497538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226497538">(Feb 16 2021 at 10:40)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123586">nagisa</span> <a href="#narrow/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code/near/226496452">said</a>:</p>
<blockquote>
<p>So that points out a couple of interesting things: first, your core _may_ be executing code in wrong mode (to make sure you would need to break on <code>__ARMv5ABSLongThunk__ZN3led6toggle17h27d172181cf2f671E</code> and disassemble before its first instruction is executed). But also, do can load your originating ELF file to let gdb know the debuginfo and symbol table of your code if you aren't already.</p>
</blockquote>
<p>As soon I step into the function, the fault occurs</p>
<div class="codehilite"><pre><span></span><code>(gdb) disass main_1
Dump of assembler code for function led::main_1:
   0x20010130 &lt;+0&gt;:     push    {r7, lr}
   0x20010132 &lt;+2&gt;:     mov     r7, sp
   0x20010134 &lt;+4&gt;:     movw    r0, #57988      ; 0xe284
   0x20010138 &lt;+8&gt;:     movs    r1, #16
   0x2001013a &lt;+10&gt;:    movt    r0, #16392      ; 0x4008
   0x2001013e &lt;+14&gt;:    str     r1, [r0, #0]
   0x20010140 &lt;+16&gt;:    movs    r0, #1
=&gt; 0x20010142 &lt;+18&gt;:    blx     0x20010178 &lt;__ARMv5ABSLongThunk__ZN3led6toggle17h27d172181cf2f671E&gt;
   0x20010146 &lt;+22&gt;:    b.n     0x20010140 &lt;led::main_1+16&gt;
End of assembler dump.
(gdb) disass 0x20010178
Dump of assembler code for function __ARMv5ABSLongThunk__ZN3led6toggle17h27d172181cf2f671E:
   0x20010178 &lt;+0&gt;:     bfcsel  0, 0x20010bb8, 2, ne
   0x2001017c &lt;+4&gt;:     lsls    r1, r1, #5
   0x2001017e &lt;+6&gt;:     movs    r0, r0
End of assembler dump.
(gdb) si

Breakpoint 3, lpc55s6x::BusFaultHandler () at /home/vn-ki/git/harsark-lpc55S69-multicore-examples/src/lib.rs:336
336     unsafe extern &quot;C&quot; fn BusFaultHandler() -&gt; ! {
</code></pre></div>



<a name="226498024"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226498024" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226498024">(Feb 16 2021 at 10:45)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123586">nagisa</span> <a href="#narrow/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code/near/226496452">said</a>:</p>
<blockquote>
<p>your core _may_ be executing code in wrong mode</p>
</blockquote>
<p>Is it possible that, my core is executing in the right mode, but the compiler (or linker) is confused and is inserting a thunk call where it's not required?</p>



<a name="226509859"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226509859" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226509859">(Feb 16 2021 at 13:01)</a>:</h4>
<p><span class="user-mention" data-user-id="355545">@vn-ki</span> the thing is that the mode switch happens based on what the call address looks like. In your particular case:</p>
<div class="codehilite"><pre><span></span><code>=&gt; 0x20010142 &lt;+18&gt;:    blx     0x20010178 &lt;__ARMv5ABSLongThunk__ZN3led6toggle17h27d172181cf2f671E&gt;
</code></pre></div>
<p>The least significant bit not being set in <code>0x20010178</code> indicates to the core that the function is an ARM code (rather than thumb) and that it should switch to that mode if not already in it.</p>



<a name="226509932"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226509932" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226509932">(Feb 16 2021 at 13:02)</a>:</h4>
<p>in contrast if the function was a thumb function the call would look like this instead:</p>
<div class="codehilite"><pre><span></span><code>=&gt; 0x20010142 &lt;+18&gt;:    blx     0x20010179 &lt;__ARMv5ABSLongThunk__ZN3led6toggle17h27d172181cf2f671E&gt;
</code></pre></div>
<p>IIRC.</p>



<a name="226510077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226510077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226510077">(Feb 16 2021 at 13:03)</a>:</h4>
<p>My question is, if it is already in thumb mode why does it need a thunk anyway? (I'm assuming it is in thumb mode, is there a way to check it?)</p>



<a name="226510178"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226510178" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226510178">(Feb 16 2021 at 13:04)</a>:</h4>
<p>Ah, looks like I'm misremembering, what I described is true for the <code>bx</code> instruction. <code>blx</code> _always_ switches mode (edit: when used with a label).</p>



<a name="226510539"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226510539" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226510539">(Feb 16 2021 at 13:08)</a>:</h4>
<p>I'm not sure why exactly a thunk is necessary.</p>
<p>You mentioned that you got a fault immediatelly after stepping into the function call. How did you do it exactly? Does it still fault if you do something like</p>
<div class="codehilite"><pre><span></span><code>br *0x20010142
continue
ni # next instruction (rather than step into
</code></pre></div>



<a name="226510617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226510617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226510617">(Feb 16 2021 at 13:09)</a>:</h4>
<p>I would guess thunk serves some purpose correctly returning from ARM mode back to thumb by using a specific return instruction? Not sure though.</p>



<a name="226510727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226510727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226510727">(Feb 16 2021 at 13:10)</a>:</h4>
<p><a href="https://community.arm.com/developer/ip-products/processors/b/processors-ip-blog/posts/branch-and-call-sequences-explained">https://community.arm.com/developer/ip-products/processors/b/processors-ip-blog/posts/branch-and-call-sequences-explained</a> describes the interworking.</p>



<a name="226510986"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226510986" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226510986">(Feb 16 2021 at 13:13)</a>:</h4>
<p>Another reason I could see a bus exception happening is your memory pages set up in some way that prevents accesses. It is worth noting that the ARM will generally have registers that more precisely describe the busfault when you end up in the exception. Worth looking into those.</p>



<a name="226511168"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226511168" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226511168">(Feb 16 2021 at 13:15)</a>:</h4>
<p>The register you're looking for is BFSR (BusFault Status Register) as documented <a href="https://developer.arm.com/documentation/100235/0004/the-cortex-m33-peripherals/system-control-block/configurable-fault-status-register">here</a></p>



<a name="226513236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226513236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226513236">(Feb 16 2021 at 13:36)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123586">nagisa</span> <a href="#narrow/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code/near/226510539">said</a>:</p>
<blockquote>
<p>I'm not sure why exactly a thunk is necessary.</p>
<p>You mentioned that you got a fault immediatelly after stepping into the function call. How did you do it exactly? Does it still fault if you do something like</p>
<p><div class="codehilite"><pre><span></span><code>br *0x20010142
continue
ni # next instruction (rather than step into
</code></pre></div><br>
</p>
</blockquote>
<p>yeah. it faults everytime. Even if there is no breakpoint, does a BusFault.</p>



<a name="226513350"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226513350" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226513350">(Feb 16 2021 at 13:37)</a>:</h4>
<p>right so if its faulting after the <code>ni</code>, then it means the <code>blx</code> itself faults, rather than anything in the function body.</p>



<a name="226513557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226513557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226513557">(Feb 16 2021 at 13:39)</a>:</h4>
<p>I would look at this point into either: can the core read this address at all (e.g. paging/MPU is set up correctly)? and is interworking working at all.</p>



<a name="226513664"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226513664" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226513664">(Feb 16 2021 at 13:40)</a>:</h4>
<p>these are just guesses though – I never dealt with interworking myself and I only barely scratched the surface with MPU a couple years ago.</p>



<a name="226513726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226513726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226513726">(Feb 16 2021 at 13:40)</a>:</h4>
<p>(at which point I found that, no, one doesn't simply enable mpu retroactively...)</p>



<a name="226513768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226513768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226513768">(Feb 16 2021 at 13:41)</a>:</h4>
<p>Thanks a lot for these pointers!! I'll thoroughly look into these.</p>



<a name="226527128"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226527128" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226527128">(Feb 16 2021 at 15:29)</a>:</h4>
<p><span class="user-mention" data-user-id="123586">@nagisa</span>  Okay, so I fixed it but the fix ended up being confusing me even more so I would really appreciate if you could maybe shed some light</p>
<p>The error was never a BusFault. It was a UsageFault. I tried really hard to make it hit the UsageFaultHandler but it's going to DefaultHandler for some reason. (I'm compiling in release mode so maybe the functions are getting optimised or something)</p>
<p>The error code was 1 which meant undefined instruction. I figured it meant that <code>blx</code> instruction is undefined. I saw in the documentation that ARMv7m does not have blx, so for fun I switched the target to ARMv7m and voila everything works perfectly fine. I double checked my board and it has 2 Cortex M33 cores which is supposed to be ARMv8<br>
<a href="/user_uploads/4715/YkZVrbmbz5LaHh3lg5NY5dG7/image.png">image.png</a></p>
<div class="message_inline_image"><a href="/user_uploads/4715/YkZVrbmbz5LaHh3lg5NY5dG7/image.png" title="image.png"><img src="/user_uploads/4715/YkZVrbmbz5LaHh3lg5NY5dG7/image.png"></a></div>



<a name="226528295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226528295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226528295">(Feb 16 2021 at 15:37)</a>:</h4>
<p>Rust can deduplicate functions, if their bodies are the same, yes.</p>



<a name="226528626"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226528626" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226528626">(Feb 16 2021 at 15:40)</a>:</h4>
<p>yeah, it seems like <code>blx label</code> is available in Thumb2 except ARMv7-M implementations.</p>



<a name="226528838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226528838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226528838">(Feb 16 2021 at 15:41)</a>:</h4>
<p>You said your primary core did work though, right? I imagine that the co-processor could actually be not m33 <span aria-label="shrug" class="emoji emoji-1f937" role="img" title="shrug">:shrug:</span></p>



<a name="226528946"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226528946" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226528946">(Feb 16 2021 at 15:42)</a>:</h4>
<p>I've seen a fair share of poorly written documentation and datasheets, and hardware bugs in components that are outside the core itself.</p>



<a name="226529711"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226529711" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226529711">(Feb 16 2021 at 15:48)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123586">nagisa</span> <a href="#narrow/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code/near/226528838">said</a>:</p>
<blockquote>
<p>You said your primary core did work though, right? I imagine that the co-processor could actually be not m33 <span aria-label="shrug" class="emoji emoji-1f937" role="img" title="shrug">:shrug:</span></p>
</blockquote>
<p>well that could be it lol. The documentation does not mention anything related to that though</p>
<blockquote>
<p>The LPC55S6x device includes a second instance of Cortex-M33. The configuration of<br>
this instance does not include MPU, FPU, DSP, ETM, Trustzone (SECEXT), Secure<br>
Attribution Unit (SAU) or co-processor interface. It supports the same debug levels and<br>
interrupt lines as the primary CPU.</p>
</blockquote>



<a name="226553077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226553077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226553077">(Feb 16 2021 at 18:15)</a>:</h4>
<p>After discussing in the rust LPC55 matrix, they think this is an LLD bug. </p>
<p><a href="https://matrix.to/#/!RcLIxSCIpjFnIgZhVz:matrix.org/$yn2AujqEx6lM5jNbxo470NGUcXslM_Wq4feDo8FQWCg?via=matrix.org&amp;via=oxide.computer&amp;via=solokeys.com">https://matrix.to/#/!RcLIxSCIpjFnIgZhVz:matrix.org/$yn2AujqEx6lM5jNbxo470NGUcXslM_Wq4feDo8FQWCg?via=matrix.org&amp;via=oxide.computer&amp;via=solokeys.com</a></p>



<a name="226553674"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Rust-lld%20generates%20wrong%20thunk%20code/near/226553674" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Rust-lld.20generates.20wrong.20thunk.20code.html#226553674">(Feb 16 2021 at 18:18)</a>:</h4>
<p>could very well be the case, but the machine code does not look wrong (if you ignore potential instruction set mismatches etc)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>